local blaze = require "blaze"
local app = require "app"
local game = app.game.running
local chat = game.chat

local ChatItem = require "club.threecard.gamelib.UI.Chat.ChatItem"
local log = blaze.logging.get("ChatUI")

local MAX_CHAT_ITEM_COUNT = 50

local M = game.ui.activity()

local function append_message(self, message)
    local chat_item = ChatItem.new(message)
    chat_item:load()
    table.insert(self.chat_items, chat_item)
    chat_item:set_parent(self.chat_items_root)
end

M.store_watchers = {
    chat = {
        ["$"] = function(self, store, state, args)
            local begin = math.max(#state.messages - MAX_CHAT_ITEM_COUNT, 1)
            for i=begin, #state.messages do
                append_message(self, state.messages[i])
            end
        end,

        ["收到聊天消息"] = function(self, store, state, args)
            local message = args
            if self.minimized then
                if not message.from_self and message.msg_type == chat.MSG_TYPE_TEXT then
                    local prefix = message.sender and message.sender.name .. ":" or ""
                    self.notify_txt.text = prefix .. message.text
                    self.animator:Play("MiniNotify", -1, 0)
                end
            else
                append_message(self, message)
                if #self.chat_items > MAX_CHAT_ITEM_COUNT then
                    local chat_item = self.chat_items[1]
                    table.remove(self.chat_items, 1)
                    chat_item:release()
                end
                if self.chat_scroll_rect.verticalScrollbar.value <= 0.0001 then
                    -- 只有在滚动条在最底部的时候才滚动到最新消息
                    self:start_coroutine(function()
                        coroutine.yield()
                        coroutine.yield() -- 必须等这2帧结束了位置才是正确的
                        self.chat_scroll_rect.verticalScrollbar.value = 0
                    end)
                end
            end
        end,

        ["清空"] = function()
            for _, item in ipairs(self.chat_items) do
                item:release()
            end
            self.chat_items = {}
        end
    }
}

function M:ctor()
    self.res = "club.threecard.gamelib/ui/chat.b:ChatUI"
    self.chat_items = {}
    local ShortcutPop = require "club.threecard.gamelib.UI.Chat.ShortcutPop"
    self.shortcut_pop = ShortcutPop.new()
    self.minimized = true
end

function M:on_injected(bind)
    bind("activity.tweener", self.animator)
    bind("button.click", self.close_mask_btn, self.on_close)
    bind("button.click", self.shortcut_btn, self.on_shortcut)
    bind("button.click", self.open_btn, self.open)
    bind("button.click", self.entry_shortcut_btn, self.on_entry_shortcut)
    bind("button.click", self.send_btn, self.on_send)
end

-- 展开聊天面板
function M:open()
    self.tweener:play("Expand")
    self.minimized = false
end

-- 收起聊天面板
function M:close()
    self:on_close()
end

function M:on_close()
    self.tweener:play("Shrink")
    self.minimized = true
end

function M:on_send()
    local text = self.input_field.text
    if #text == 0 then
        log.debug("不能发送空消息")
        return
    end
    local cooldown = game.store.chat.state.cooldown
    if cooldown > 0 then
        -- 发送消息冷却中
        log.debug(string.format("剩余冷却时间：%s", cooldown))
        return
    end
    game.store.chat.say_text(text)
    self.input_field.text = ""
end

function M:on_shortcut()
    self.shortcut_pop:set_parent(self.shortcut_pop_root)
    self.shortcut_pop:load()
    self.shortcut_pop:show()
end

function M:on_entry_shortcut()
    self.shortcut_pop:set_parent(self.entry_shortcut_pop_root)
    self.shortcut_pop:load()
    self.shortcut_pop:show()
end

function M:on_release()
    self:super("on_release")
    self.shortcut_pop:release()
end

return M